/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//asdfas
package testeinterface;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class PTipoRequisito extends JPanel{
	        
        public PTipoRequisito(){            
            this.setLayout(new GridLayout(3,2));
            
           final JTextField tfCodigo = new JTextField("",20);
           final JTextField tfNome   = new JTextField("",20);
           JButton btInserir      = new JButton("Inserir");
           JLabel lbCodigo = new JLabel("Código");
           JLabel lbNome   = new JLabel("Nome");
            
            
            btInserir      = new JButton("Inserir");
            
            btInserir.addActionListener (new ActionListener () {  
                 public void  actionPerformed(ActionEvent e){  
                     
                     Logger.gravar(Logger.INICIALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                     
                      try{
                        String host = "jdbc:mysql://143.106.150.216:3306/SatodaDB?zeroDateTimeBehavior=convertToNull";
                        String uName = "satoda";
                        String uPass = "adotas2014";
                        Connection con = DriverManager.getConnection(host, uName, uPass);
                        
                        Statement stmt = con.createStatement( );
                        String SQL = "INSERT INTO T_TIPOREQUISITO(codigo, nome) VALUES ("+ tfCodigo.getText() +", '"+ tfNome.getText() +"')";
                        stmt.executeUpdate( SQL );
                    }
                    catch( SQLException err ){
                        Logger.gravar(Logger.ERRO, getNomeClasse() + ".actionPerformed(ActionEvent e): " + err.getMessage());
                        System.out.println(err.getMessage());
                    }
                      
                    Logger.gravar(Logger.FINALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                    System.out.println("Tipo do requisito cadastrado com sucesso!");  
                 }  
              });  
            
            this.add(lbCodigo);
            this.add(tfCodigo);
            
            this.add(lbNome);
            this.add(tfNome);
            
            this.add(btInserir);
        }
        
        private String getNomeClasse(){
            return this.getClass().getName();
        }
}

